package com.cy.decorator;
/**
 * <br>@class: Decorator
 * <br>@description: 装饰器
 * <br>@author: cy
 * <br>@date: 2020-03-29 15:52:46
 * <br>@version: 1.0
 * <br>
 * <br>@since: (版本) 作者 时间 注释
 */
public class Decorator {
    public static void main(String[] args) {
     //装饰器Decorator，在不改变原有对象的基础上，将功能附加到对象上
        IRobot robot = new Robot();
        robot.run();
        robot.atk();
        System.out.println("----------装饰后v2-----------");
        robot = new RobotV2(robot);
        robot.run();
        robot.atk();
        System.out.println("----------装饰后v3-----------");
        robot = new RobotV3(robot) ;
        robot.run();
        robot.atk();
    }
}
interface IRobot{
    void run();
    void atk();
}
class Robot implements IRobot  {

    @Override
    public void run() {
        System.out.println("移动");
    }

    @Override
    public void atk() {
        System.out.println("攻击");
    }

}
//装饰者
abstract class RobotDecorator implements IRobot{
    protected IRobot iRobot;

    public RobotDecorator(IRobot iRobot) {
        this.iRobot = iRobot;
    }
}
//装饰实现
class RobotV2 extends RobotDecorator{

    public RobotV2(IRobot iRobot) {
        super(iRobot);
    }

    @Override
    public void run() {
        iRobot.run();
    }

    @Override
    public void atk() {
        iRobot.atk();
        System.out.println("发射火箭");
    }

}
class RobotV3 extends RobotDecorator{

    public RobotV3(IRobot iRobot) {
        super(iRobot);
    }

    @Override
    public void run() {
        iRobot.run();
        System.out.println("快速移动");
    }

    @Override
    public void atk() {
        iRobot.atk();
    }

}